<?php

class Upload extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model('up_model');
        $this->load->model('news_model');
        $this->load->helper('url');
    }

    public function index()
    {
        $this->load->view('upload_form', array('error' => ' '));
    }
   /* public function do_upload()
    {
        // echo "a";
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 1000;
        $config['max_width'] = 10240;
        $config['max_height'] = 7680;

        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $name = $this->input->post('fname');

        if (!$this->upload->do_upload('userfile')) {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        } else {
//            $data['upload_data']=$this->upload->data();  //文件的一些信息
//            foreach($data['upload_data'] as $item => $value){
//                echo $item.":".$value."<br>";
//            }
            if ($this->input->post('checkbox')) {//判断是否是精选
                $a = 1;
            } else {
                $a = 0;
            }
            if ($this->input->post('select') == 0 || $this->input->post('fname') == null) {//判断名字和类型是否选择
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_form', $error);
            } else {//上传数据
                $data = array('upload_data' => $this->upload->data());
                $up = array(
                    'name' => $name,//名字
                    'img_path' => substr($config['upload_path'], 1) . $data['upload_data']['file_name'],//相对路径
                    'cat_id' => $this->input->post('select'),//classfiy
                    'is_best' => $a//is_best
                );
                $this->up_model->save($up);//上传
                $this->load->view('pages/upload', $data);
            }
        }
    }*/
    public function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = 1000;
        $config['max_width'] = 10240;
        $config['max_height'] = 7680;
        $config['max_height'] = 7680;

        $this->load->library('upload', $config);
        $this->upload->initialize($config);
        $name = $this->input->post('fname[]');
        $select = $this->input->post('select[]');
        $checkbox = $this->input->post('checkbox[]');
        $post_file_ary = $_FILES['userfile'];
        $para_file_ary = array();
        $i=0;
        foreach($post_file_ary['name'] as $key=>$val)
        {
            $para_file_ary['name'] = $val;
            $para_file_ary['type'] = $post_file_ary['type'][$key];
            $para_file_ary['tmp_name'] = $post_file_ary['tmp_name'][$key];
            $para_file_ary['error'] = $post_file_ary['error'][$key];
            $para_file_ary['size'] = $post_file_ary['size'][$key];

            if (!$this->upload->do_upload($para_file_ary)) {
                $error = array('error' => $this->upload->display_errors());

                $this->load->view('upload_form', $error);
            } else {
                if ($checkbox[$i]) {//判断是否是精选
                    $a = 1;
                } else {
                    $a = 0;
                }
                if ($select[$i] == 0 || $name[$i] == null) {//判断名字和类型是否选择
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('upload_form', $error);
                } else {//上传数据
                    $data = array('upload_data' => $this->upload->data());
                    $up = array(
                        'name' => $name[$i],//名字
                        'img_path' => substr($config['upload_path'], 1) . $data['upload_data']['file_name'],//相对路径
                        'cat_id' => $select[$i],//classfiy
                        'is_best' => $a//is_best
                    );
                    $this->up_model->save($up);//上传
                }
            }
            $i++;

        }
        $data['arr'] = $this->news_model->ims_punch_cat(1);
        $data['arr1'] = $this->news_model->ims_punch_cat(2);
        // echo json_encode($data);
        $this->load->view('pages/upload',$data);
    }
}
?>